﻿@{
    ViewBag.Title = Model.siteName+" - Thanh toán";
    ViewBag.Description = Model.htmlDescription;
    ViewBag.Keywords = Model.htmlKeywords;
}
@model BDShop.Models.OrderModels
@using BDShop.Business.Cache;
@using BDShop.Entities;
@using BDShop.Configuration;
@using BDShop.Framework.Extensions;
@using BDShop.Utilities.Managers;
@using BDShop.Web;

<!--Middle Part Start-->
@using (Html.BeginForm("CheckOut", "Cart", FormMethod.Post, new { id = "frmCheckOut" }))
{@Html.AntiForgeryToken()
    <div id="content">
        <!--Breadcrumb Part Start-->
        <div class="breadcrumb"><a href="/">Trang chủ</a> » Thanh toán</div>
        <!--Breadcrumb Part End-->
        <h1>Thanh toán</h1>
         @Html.ValidationSummary(true, "", new { @class = "error" })<br/>
        <div class="checkout">
            <div class="checkout-heading">Thông tin giao hàng</div>
            <div class="checkout-content">
                <table class="form">
                    <tbody>
                        <tr>
                            <td><span class="required">*</span> Họ và tên:</td>
                            <td>
                                @Html.TextBoxFor(m=>m.fullName,new{@class="large-field",@id="fullname",@disabled="disabled"})
                               &nbsp;
                                <label id="fullname-error" class="error valid" for="fullname"></label>
                            </td>
                        </tr>
                        <tr>
                            <td><span class="required" id="payment-postcode-required">*</span> Số điện thoại:</td>
                            <td>
                                @Html.TextBoxFor(m=>m.mobile,new{@class="large-field",@id="mobile"})
                                &nbsp;
                                <label id="mobile-error" class="error valid" for="mobile"></label>
                            </td>
                        </tr>
                        <tr>
                            <td><span class="required">*</span> Giới tính:</td>
                            <td>
                                <select class="large-field" name="gender" id="gender" disabled="disabled">
                                    <option value="">--- Vui lòng chọn --- </option>
                                    <option value="1" @((Model.gender==1)?"selected='selected'":"")>Nam</option>
                                    <option value="0" @((Model.gender==0)?"selected='selected'":"")>Nữ</option>
                                </select>
                                &nbsp;
                                <label id="gender-error" class="error valid" for="gender"></label>
                            </td>
                        </tr>
                        <tr>
                            <td><span class="required">*</span> Tỉnh / Thành phố:</td>
                            <td>
                                @Html.DropDownListFor(m=>m.city,(IEnumerable<SelectListItem>)ViewBag.City,new{@class="large-field",@id="city"})
                                &nbsp;
                                <label id="city-error" class="error valid" for="city"></label>
                            </td>
                        </tr>
                        <tr>
                            <td><span class="required">*</span> Quận / Huyện:</td>
                            <td>
                                <select class="large-field" name="district" id="district">
                                    <option value="">--- Vui lòng chọn --- </option>
                                </select>
                                &nbsp;
                                <label id="district-error" class="error valid" for="district"></label>
                            </td>
                        </tr>
                        <tr>
                            <td><span class="required">*</span> Địa chỉ:</td>
                            <td>
                                @Html.TextBoxFor(m=>m.address,new{@class="large-field",@id="address"})
                                &nbsp;
                                <label id="address-error" class="error valid" for="address"></label>
                            </td>
                        </tr>

                    </tbody>
                </table>
            </div>
        </div>

        <div class="checkout">
            <div class="checkout-heading">Phương thức thanh toán</div>
            <div class="checkout-content">
                <p>Vui lòng chọn phương thức thanh toán.</p>
                <table class="radio">
                    <tbody>
                        <tr class="highlight">
                            <td>
                                <label>
                                    <input type="radio" checked="checked" id="chkCash" value="1" name="paymentMethod">
                                    Thanh toán khi nhận hàng</label></td>
                        </tr>
                        <tr class="highlight">
                            <td>
                                <label>
                                    <input type="radio" id="chkTransfer" value="2" name="paymentMethod">
                                    Chuyển khoản</label></td>
                        </tr>
                    </tbody>
                </table>

            </div>
        </div>

        <div class="checkout">
            <div class="checkout-heading">Thông tin đơn hàng</div>
            <div class="checkout-content">
                <div class="checkout-product">
                    <table>
                        <thead>
                            <tr>
                                <td>Hình ảnh</td>
                                <td class="name">Tên sản phẩm</td>
                                <td class="model">Model</td>
                                <td class="quantity">Số lượng</td>
                                <td class="price">Giá</td>
                                <td class="total">Tổng cộng</td>
                            </tr>
                        </thead>
                        <tbody>
                            @{ int sum = 0; }
                            @foreach (var order in (List<BDShop.Models.OrderDetailModels>)ViewBag.OrderDetail)
                            {
                                ProductCacheComplex product = ProductCache.Instance.Get().Where(p => p.ID == order.productID).FirstOrDefault();
                                Constant property = ConstantCache.Instance.Get().Where(c => c.Value == order.propertyID).FirstOrDefault();
                                int total = order.price * order.quantity;
                                sum += total;

                                string[] categoryDefinition = null;
                                if (!string.IsNullOrEmpty(product.CategoryDefinition))
                                {
                                    categoryDefinition = product.CategoryDefinition.Split('|')[0].Split('#');
                                }
                                var url = UrlManager.ProductDetailUrl(product.ID, product.Name, categoryDefinition);
                                <tr>
                                    <td class="image"><a href="@url">
                                        <img style="width: 45px" class="lazy" data-original="@Path.Combine(BDShopConfiguration.Storage.MainImagePath, product.MainImage)" src="@Path.Combine(BDShopConfiguration.Storage.MainImagePath, product.MainImage)" alt="@product.Name" /></a></td>
                                    <td class="name"><a href="@url">@product.Name</a></td>
                                    <td class="model">@property.Description</td>
                                    <td class="quantity">@order.quantity</td>
                                    <td class="price">@product.Price.ToCurrencyString(false, true)</td>
                                    <td class="total">@total.ToCurrencyString(false, true)</td>
                                </tr>
                            }
                        </tbody>
                        <tfoot>
                            <tr>
                                @{
                            int fee = 20000;
                            sum += fee;
                                }
                                <td class="price" colspan="5"><b>Chi phí vận chuyển:</b></td>
                                <td class="total">@fee.ToCurrencyString(false, true)</td>
                                @Html.Hidden("shippingcost",fee)
                            </tr>
                            <tr>
                                <td class="price" colspan="5"><b>Thành tiền:</b></td>
                                <td class="total">@sum.ToCurrencyString(false, true)</td>
                                @Html.Hidden("total",sum)
                            </tr>
                        </tfoot>
                    </table>
                </div>
                <div class="buttons">
                    <div class="right">
                        <input type="submit" class="button" id="button-confirm" value="Mua hàng">
                    </div>
                </div>
            </div>
        </div>
    </div>
}
<!--Middle Part End-->
<div class="clear"></div>

<script>

    $("#frmCheckOut").validate({
        rules: {
            fullname: {
                required: true
            },
            mobile: {
                required: true,
                numericOnly: true,
                minlength: 10,
                maxlength: 15
            },
            gender: {
                required: true
            },
            city: {
                required: true
            },
            district: {
                required: true
            },
            address: {
                required: true
            }
        },
        messages: {
            fullname: "Vui lòng nhập họ tên đầy đủ.",
            mobile: "Số điện thoại không hợp lệ.",
            gender: "Vui lòng chọn giới tính.",
            city: "Vui lòng chọn Tỉnh/Thành phố.",
            district: "Vui lòng chọn Quận/Huyện.",
            address: "Vui lòng nhập địa chỉ."
        }
    });

    $.validator.addMethod("numericOnly", function (value) {
        return /^[0-9]+$/.test(value);
    }, '');
</script>
<script>
    $(document).ready(function() {
              $('#city').change(function() {
                    $('#district').html('');
                    $.getJSON('@Url.Action("GetDistrict","Cart")', { cityID:$('#city').val() }, function(data) {
                        $.each(data, function (i, item) {
                                $('#district').append('<option value="' + item.Value + '">' + item.Text + '</option>');
                              });
                        });
                  });
           });
</script>